﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Castle.ActiveRecord;
using System.ComponentModel;
using Light.Utility;

namespace Light.Framework.Model
{
    /// <summary>
    /// 角色字典实体
    /// </summary>
    [Traceable]
    [ActiveRecord("Roles")]
    [DisplayName("角色")]
    public class Role : Entity
    {

        [Property("FName")]
        [DisplayName("名称")]
        public string Name { get; set; }

        [Property]
        [DisplayName("备注")]
        public string Remark { get; set; }

        [Property]
        [DisplayName("系统角色")]
        public bool IsSystem { get; set; }

        /// <summary>
        /// 显示顺序
        /// </summary>
        [Property]
        public int DisplayOrder { get; set; }

        /// <summary>
        /// 是否为超级管理员角色
        /// </summary>
        [Property]
        public bool IsSuper { get; set; }

        private IList tasks = new List<Task>();
        [HasAndBelongsToMany(typeof(Task),
                Table = "R_Role_Task",
                ColumnRef = " Task_ID ",
                ColumnKey = " Role_ID ")]
        public IList Tasks
        {
            get { return tasks; }
            set { tasks = value; }
        }

        public bool HasTask(string taskCode)
        {
            if (String.IsNullOrEmpty(taskCode))
                return true;

            string[] codes = taskCode.Split(',');

            foreach (Task task in tasks)
            {
                foreach (var code in codes)
                {
                    if (task.Code.ToUpper() == code.Trim().ToUpper())
                        return true;
                }
            }
            return false;
        }

        public bool HasTask(Task task)
        {
            return tasks.Contains(task);
        }


        public override bool Equals(object obj)
        {
            return ((Role)obj).ID == this.ID;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
